package com.demo.studentmanager;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.demo.studentmanager.databinding.ActivityUserManagerBinding;
import com.example.applicationdemo.R;

import java.util.ArrayList;
import java.util.List;

public class StudentManagerFragment extends Fragment {
    ActivityUserManagerBinding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_user_manager, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding = ActivityUserManagerBinding.bind(view);

        binding.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(requireActivity(), RegisterActivity.class));
            }
        });
        binding.rvStudent.setAdapter(adapter);
        binding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userList.clear();
                userList.addAll(new DBHelper(requireActivity()).getUser(binding.etSearch.getText().toString()));
                adapter.notifyDataSetChanged();
            }
        });
        binding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().isEmpty()) {
                    userList.clear();
                    userList.addAll(new DBHelper(requireActivity()).getUser(""));
                    adapter.notifyDataSetChanged();
                }
            }
        });
        binding.etSearch.setText("");

    }

    private List<User> userList = new ArrayList<>();
    private StudentAdapter adapter = new StudentAdapter(userList);




}